package com.zidongxiangxi.rabbit.producer.manager;

import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * RabbitTemplate管理类
 *
 * @author chenxudong
 * @date 2020/11/07
 */
public class RabbitTemplateManager {
	private final Map<String, RabbitTemplate> rabbitTemplateMap = new ConcurrentHashMap<>();

	public void putRabbitTemplate(RabbitTemplate rabbitTemplate) {
		ConnectionFactory connectionFactory = rabbitTemplate.getConnectionFactory();
		String host = connectionFactory.getHost();
		int port = connectionFactory.getPort();
		String virtualHost = connectionFactory.getVirtualHost();

		String key = host + "_" + port + "_" + virtualHost;
		rabbitTemplateMap.put(key, rabbitTemplate);
	}

	public RabbitTemplate getRabbitTemplate(String host, int port, String virtualHost) {
		String key = host + "_" + port + "_" + virtualHost;
		return rabbitTemplateMap.get(key);
	}
}
